<?php
declare (strict_types = 1);

namespace app\course\service\live\admin;

use app\admin\logic\system\Log;
use app\common\library\Excel;
use app\common\model\payment\admin\PaymentOrder;
use app\common\model\payment\admin\PaymentTransaction;
use app\common\service\BaseService;
use app\course\logic\live\admin\LiveOrder as OrderLogic;
use eduline\admin\libs\pageform\FormItem;
use eduline\admin\libs\pagelist\ListItem;
use eduline\admin\page\PageList;
use think\facade\Request;

/**
 * 大班课课程订单接口类-后台
 */
class LiveOrder extends BaseService
{
    /**
     * 订单列表
     * Author   Martinsun<syh@sunyonghong.com>
     * Date 2020-07-04
     */
    public function index()
    {
        // 搜索功能
        $map                                          = $withJoin                                          = [];
        Request::has('mhm_id', 'get', true) && $map[] = ['__TABLE__.mhm_id', '=', Request::get('mhm_id/d')];
        Request::has('from', 'get', true) && $map[]   = ['__TABLE__.from', '=', Request::get('from/s')];
        Request::has('status', 'get', true) && $map[] = ['pdorder.status', '=', Request::get('status/d')];
         Request::has('payment_type', 'get', true) && $map[] = ['transaction.payment_type', '=', Request::get('payment_type/s')];
        Request::has('phone', 'get', true) && $map[]  = ['user.phone', '=', Request::get('phone/s')];
        if (Request::has('time', 'get', true)) {
            $time  = Request::get('time/a');
            $stime = strtotime(date('Y-m-d 00:00:00', strtotime($time[0])));
            $etime = strtotime(date('Y-m-d 00:00:00', strtotime($time[1]))) + 86399;
            $map[] = ['__TABLE__.create_time', 'between', [$stime, $etime]];
        }
        Request::has('order_sub_no', 'get', true) && $map[] = ['__TABLE__.order_sub_no', '=', Request::get('order_sub_no/s')];
        $data                                               = app(OrderLogic::class)->getList($map)->bindAttr('user', ['phone', 'user_name'])->bindAttr('transaction', ['payment_type_text', 'payment_time'])->hidden(['user', 'transaction', 'pdorder']);
        $is_platform                                        = request()->is_platform;
        // 定义字段
        $keyList = [
            'order_sub_no'      => ListItem::make()->title('订单号'),
            'product_title'     => ListItem::make('custom')->title('课程名'),
            'school_name'       => ListItem::make()->title('机构')->visible($is_platform),
            'price'             => ListItem::make('custom')->title('价格'),
            'discount'          => ListItem::make('custom')->title('优惠'),
            // 'payment'           => ListItem::make('custom')->title('订单价格'),
            'phone'             => ListItem::make()->title('购买者'),
            'create_time'       => ListItem::make()->title('下单时间'),
            'payment_time'      => ListItem::make()->title('支付时间'),
            'payment_type_text' => ListItem::make()->title('支付方式'),
            'from'              => ListItem::make()->title('订单来源'),
            'status_text'       => ListItem::make()->title('订单状态'),
        ];

        // 支付状态
        $status = app(PaymentOrder::class)->getStatusOptions();
        // 支付方式
        $paymentType = app(PaymentTransaction::class)->getPaymentTypeOptions();
        // 来源
        $from = app(OrderLogic::class)->getFromOptions();

        $searchKey = [
            'mhm_id'       => FormItem::make('searchSchool')->title(false)->visible($is_platform),
            'order_sub_no' => FormItem::make()->title(false)->placeholder('订单号'),
            'phone'        => FormItem::make()->title(false)->placeholder('购买者手机号')->maxlength(30),
            'status'       => FormItem::make('select')->title(false)->placeholder('订单状态')->options($status),
            'payment_type' => FormItem::make('select')->title(false)->placeholder('支付方式')->options($paymentType),
            'from'         => FormItem::make('select')->title(false)->placeholder('来源')->options($from),
            'time'         => FormItem::make('date')->title(false)->dateType(FormItem::option('DateType', 'DATE_RANGE'))->startPlaceholder('下单开始时间')->endPlaceholder('下单截止时间')->format('yyyy-MM-dd'),
        ];
        // 设置表单
        $list = app(PageList::class);
        // 表单字段
        $list->pageKey   = $keyList;
        $list->searchKey = $searchKey;
        $list->datas     = $data;

        return $list->send();
    }

    /**
     * 订单导出
     * Author   Martinsun<syh@sunyonghong.com>
     * Date 2020-03-23
     */
    public function export()
    {
        // 搜索功能
        $map = [];
        Request::has('mhm_id', 'post', true) && $map[] = ['__TABLE__.mhm_id', '=', Request::post('mhm_id/d')];
        Request::has('from', 'post', true) && $map[] = ['__TABLE__.from', '=', Request::post('from/s')];
        Request::has('status', 'post', true) && $map[] = ['pdorder.status', '=', Request::post('status/d')];
        Request::has('payment_type', 'post', true) && $map[] = ['transaction.payment_type', '=', Request::post('payment_type/s')];
        Request::has('phone', 'post', true) && $map[] = ['user.phone', '=', Request::post('phone/s')];
        if (Request::has('time', 'post', true)) {
            $time  = Request::post('time/a');
            $stime = strtotime(date('Y-m-d 00:00:00', strtotime($time[0])));
            $etime = strtotime(date('Y-m-d 00:00:00', strtotime($time[1]))) + 86399;
            $map[] = ['__TABLE__.create_time', 'between', [$stime, $etime]];
        }
        Request::has('order_sub_no', 'post', true) && $map[] = ['__TABLE__.order_sub_no', '=', Request::post('order_sub_no/s')];

        bcscale(2);
        $data = app(OrderLogic::class)
            ->with(['transaction', 'school', 'user', 'pdorder', 'course_title'])
            ->withJoin(['user', 'pdorder', 'transaction', 'course' => ['course.course_live_type' => 'live_type']], 'LEFT')
            ->where($map)->where('course.course_live_type', 1)
            ->order('update_time', 'DESC')
            ->select()
            ->each(function ($i){
                $i->minus_price = bcsub($i->original_price, $i->payment);
            })
            ->bindAttr('user', ['phone', 'user_name'])
            ->bindAttr('transaction', ['payment_type_text', 'payment_time', 'out_trade_no'])
            ->hidden(['user', 'transaction', 'pdorder']);

        $xlsCell = [
            ['order_sub_no', '订单号'],
            ['product_title', '课程名'],
            ['school_name', '机构'],
            ['original_price', '原价'],
            ['payment', '支付金额'],
            ['minus_price', '优惠'],
            ['user_name', '购买者'],
            ['create_time', '下单时间'],
            ['payment_time', '支付时间'],
            ['payment_type_text', '支付方式'],
            ['out_trade_no', '商户订单'],
            ['from', '订单来源'],
            ['status_text', '订单状态']
        ];

        $is_platform = request()->is_platform;
        if (!$is_platform) unset($xlsCell['school_name']);

        Log::write('导出大班课订单', 'live_order', 'export', true);

        return Excel::exportExcel('大班课订单', $xlsCell, $data);
    }
}
